﻿<%@ Application Language="C#" %>
<%@ Import Namespace="Framework.Library.Core.Config" %>
<%@ Import Namespace="Framework.Library.Data.DAL" %>
<%@ Import Namespace="Framework.Library.Data.DAO" %>
<%@ Import Namespace="log4net" %>
<%@ Import Namespace="System.Reflection" %>

<script runat="server">
    protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    private void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["So_Nguoi_Online"] = 0;
        //log4net.Config.XmlConfigurator.Configure();                
    }

    private void Application_BeginRequest(object sender, EventArgs e)
    {
    }

    private void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown
        Application.Lock();

        Application["So_Nguoi_Online"] = int.Parse(Application["So_Nguoi_Online"].ToString()) - 1;

        Application.UnLock();
    }

    private void Application_Error(object sender, EventArgs e)
    {
        //log.Error("this error");
        // Code that runs when an unhandled error occurs
        //Exception objErr = Server.GetLastError().GetBaseException();
        //Session["objErr"] = objErr;
        //Session["objUrl"] = Request.Url.ToString();
        //Response.Redirect("/Error.aspx");
    }

    private void Session_Start(object sender, EventArgs e)
    {
        //log.Error("this start");
        // Code that runs when a new session is started
        Application.Lock();
        Application["So_Nguoi_Online"] = int.Parse(Application["So_Nguoi_Online"].ToString()) + 1;

        //Lấy thông tin người sử dụng
        try
        {
            PortalStatistic info = new PortalStatistic();
            Configurations cfg = new Configurations();
            if (!Configurations.DEBUG_MODE)
            {                
                info.DATETIME = DateTime.Now;
                info.IP = Request.ServerVariables["REMOTE_ADDR"].Trim();              
                info.PORTALID = Configurations.PORTALID;
                ManagerFactory.DAOEntity.Insert(info);
            }
        }
        catch (Exception exception)
        {
            Framework.Library.Core.Provider.Log.Logger.Error(exception.Message); 
        }
        Application.UnLock();
    }

    private void Session_End(object sender, EventArgs e)
    {
        Application.Lock();
        Application["So_Nguoi_Online"] = int.Parse(Application["So_Nguoi_Online"].ToString()) - 1;
        Application.UnLock();
    }

</script>
